<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/6/8
 * Time: 12:25
 */

namespace Admin\Controller;

use Think\Controller;

class SearchController extends Controller
{
    public function dateSearch()
    {
        $start = strtotime($_POST["start_date"]);
        $end = strtotime($_POST["end_date"]);
        if ($start != null && $end != null) {
            $obj = M("order_profile");
            $obj2 = M("user_main");
            $user = session("user");
            $list = $obj->where("(order_time between " . $start . " AND " . $end . " and status =1) and belong_gc='" . $user . "'")->order("order_time desc")->select();
            $datas = [];
            foreach ($list as $l) {
                $uid = $l["uid"];
                $arr = $obj2->where("uid=" . $uid)->find();
                $data["name"] = $arr["name"];
                $data["orderNum2"] = $l["ordernum2"];
                $data["xf_type"] = $l["xf_type"];
                $data["sex"] = $l["sex"];
                $data["num"] = $l["num"];
                $data["order_time"] = $l["order_time"];
                $data["status"] = $l["status"];
                $data["oid"] = $l["oid"];
                $data["belong"] = $l["belong"];
                $data["color"] = $l["color"];
                $data["collar_type"] = $l["collar_type"];
                $data["sleeve_type"] = $l["sleeve_type"];
                $data["front_fly"] = $l["front_fly"];
                $data["pocket"] = $l["pocket"];
                $data["type"] = $l["type"];
                $data["descr2"] = $l["descr2"];
                //西服
                $data["xf_num"] = $l["xf_num"];
                $data["xf_color"] = $l["xf_color"];
                $data["lapel"] = $l["lapel"];
                $data["jag"] = $l["jag"];
                $data["sleeve_fork"] = $l["sleeve_fork"];
                $data["xf_pocket"] = $l["xf_pocket"];
                $data["fastener"] = $l["fastener"];
                $data["zuo"] = $l["zuo"];

                $data["pro_id"] = $l["pro_id"];
                $data["uid"] = $uid;
                $datas[] = $data;
            }
            $this->assign('datas', $datas);
            if (isMobile()) {
                $this->display("Order/orderList4_m");
            } else {
                $this->display("Order/orderList4");
            }
        } else {
            $desc = "请选择日期，在搜索";
            $this->assign("desc", $desc);
            if (isMobile()) {
                $this->display("Order/orderList4_m");
            } else {
                $this->display("Order/orderList4");
            }
        }
    }

    //工厂列表搜索
    public function gcOrderSearch()
    {
        $postData = I("post.");
        if ($postData["ifin"] != null) {
            $obj = M("order_profile");
            $obj2 = M("user_main");
            $user = session("user");
            $mainArr = $obj2->where("name like '" . $postData["ifin"] . "%'")->find();
            if(empty($mainArr)){

            }else{
            $list = $obj->where("uid=" . $mainArr["uid"] . " and belong_gc='" . $user . "'")->order("order_time desc")->select();
            $datas = [];
            foreach ($list as $l) {
                $uid = $l["uid"];
                $arr = $obj2->where("uid=" . $uid)->find();
                $data["name"] = $arr["name"];
                $data["orderNum2"] = $l["ordernum2"];
                $data["xf_type"] = $l["xf_type"];
                $data["sex"] = $l["sex"];
                $data["num"] = $l["num"];
                $data["order_time"] = $l["order_time"];
                $data["status"] = $l["status"];
                $data["oid"] = $l["oid"];
                $data["belong"] = $l["belong"];
                $data["color"] = $l["color"];
                $data["collar_type"] = $l["collar_type"];
                $data["sleeve_type"] = $l["sleeve_type"];
                $data["front_fly"] = $l["front_fly"];
                $data["pocket"] = $l["pocket"];
                $data["type"] = $l["type"];
                $data["descr2"] = $l["descr2"];
                //西服
                $data["xf_num"] = $l["xf_num"];
                $data["xf_color"] = $l["xf_color"];
                $data["lapel"] = $l["lapel"];
                $data["jag"] = $l["jag"];
                $data["sleeve_fork"] = $l["sleeve_fork"];
                $data["xf_pocket"] = $l["xf_pocket"];
                $data["fastener"] = $l["fastener"];
                $data["zuo"] = $l["zuo"];

                $data["pro_id"] = $l["pro_id"];
                $data["uid"] = $uid;
                $datas[] = $data;
            }

            $this->assign('datas', $datas);
            }
            if (isMobile()) {
                $this->display("Order/orderList4_m");
            } else {
                $this->display("Order/orderList4");
            }
        } else {
            $desc = "请输入名称";
            $this->assign("desc", $desc);
            if (isMobile()) {
                $this->display("Order/orderList4_m");
            } else {
                $this->display("Order/orderList4");
            }
        }
    }

    //面料搜索
    public function colorSearch()
    {
        $data = I("post.");
        $obj = M("pro_color");
        $count = $obj->where("color like '" . $data["ifin"] . "%'")->count();
        $p = getPage($count);
        $list = $obj->where("color like '" . $data["ifin"] . "%'")->limit($p->firstRow, $p->listRows)->select();

        $this->assign("page", $p->show());
        $this->assign('datas', $list);

        if (isMobile()) {
            $this->display("colorList_m");
        } else {
            $this->display("Basic/colorList");
        }
    }

    //收款条件搜索
    public function collectionSearch()
    {
        if (IS_POST) {
            $data = I("post.");
            if ($data["select"] != null) {
                $obj = M("order_main");
                $user = M("user_main");
                $orderp = M("order_profile");
                $money = M("order_money");
                if ($data["select"] == "name") {

                    $mainArr = $user->query("SELECT * FROM user_main a LEFT JOIN order_main b ON a.uid=b.uid WHERE a.name LIKE '" . $data["ifin"] . "%'");

                } elseif ($data["select"] == "ordernum") {

                    $mainArr = $user->query("SELECT * FROM user_main a LEFT JOIN order_main b ON a.uid=b.uid WHERE b.orderNum LIKE '" . $data["ifin"] . "%'");

                }

                $datas = [];
                $arr2 = [];
                foreach ($mainArr as $main) {
                    $arr2["omid"] = $main["omid"];
                    $arr2["gc_updated"] = $main["gc_updated"];
                    //查询名字
                    $userArr = $user->where("uid =" . $main["uid"])->find();

                    $arr2["name"] = $userArr["name"];
                    $arr2["ordernum"] = $main["ordernum"];

                    //查询衣服类型
                    $orderpArr = $orderp->where("omid=" . $main["omid"])->select();
                    $type = [];
                    foreach ($orderpArr as $p) {
                        $type[] = $p["xf_type"];
                    }
                    $arr2["type"] = implode("+", $type);

                    $arr2["num"] = $main["count"];
                    //查询order_money表
                    $moneyArr = $money->where("omid=" . $main["omid"])->find();
                    $arr2["price_count"] = $main["price_count"];//总金额
                    $arr2["dj_count"] = $main["dj_count"];//定金
                    $arr2["dj_ren"] = $moneyArr["dj_ren"];
                    $arr2["dj_updated"] = $moneyArr["dj_updated"];
                    $arr2["dj_type"] = $moneyArr["dj_type"];
                    $arr2["balance"] = $main["price_count"] - $main["dj_count"]; //余额
                    $arr2["ba_ren"] = $moneyArr["ba_ren"]; //余额收款人
                    $arr2["ba_updated"] = $moneyArr["ba_updated"]; //余额日期
                    $arr2["ba_type"] = $moneyArr["ba_type"]; //余额方式

                    $datas[] = $arr2;
                }
                $this->assign("datas", $datas);
                $this->display("Finance/collectionList");
            } else {
                $desc = "请输入名称";
                $this->assign("desc", $desc);
                $this->display("Finance/collectionList");
            }
        } else {
            $this->error("非法请求");
        }
    }
} 